#!/bin/bash -e

#if [ $# -ne 4 ]; then
#    echo "Usage: $0 <mysql_username> <mysql_password> <mysql_host> <mysql_port>"
#    exit 1
#fi

HOST='120.92.77.212'
USER='ubuntu'
NOW_TIMESTAMP=`date +%s`
DATE=`date -d @$NOW_TIMESTAMP +"%Y%m%d"`
HOUR=`date -d @$NOW_TIMESTAMP +"%H"`
OSS_PATH=/data/log/$DATE/$HOUR/
LOG_NAME='world_oss*'
PWD=`pwd`
WORK_DIR="$PWD/oss_log/20180117/"
SOURCE_DIR=$WORK_DIR"source/"
CSV_DIR=$WORK_DIR"csv/"

# mysql relation
OSS_DATABASE=oss
MYSQL_USENAME=$1
MYSQL_PASSWD=$2
MYSQL_HOST=$3
MYSQL_PORT=$4


if [ ! -d $WORK_DIR ]; then
    mkdir $WORK_DIR -p
fi

if [ ! -d $SOURCE_DIR ]; then
    mkdir $SOURCE_DIR
fi

#scp $USER@$HOST:$OSS_PATH$LOG_NAME $SOURCE_DIR

rm -fr $CSV_DIR
mkdir $CSV_DIR

cd $WORK_DIR

for file in `find $WORK_DIR -name 'world_oss*'`; do
    echo "splitting oss log file: $file..."
    awk -F'|' '{print >> "csv/"$1".csv"}' $file
    echo "oss log file: $file has been split."
done

#ls  | xargs -i{} awk -F '|' '{print >> "../csv/"$1".csv"}' {}

cd $CSV_DIR
for file in `ls *.csv`; do
    echo "removing prefix of $file..."
    rm -f tmp.file
    cut -d'|' -f2- $file > tmp.file
    mv tmp.file $file
    echo "prefix of $file has been removed."
done

for file in `ls *.csv`; do
    OSS_TABLE=`basename $file .csv`
    echo "importing $file into database: $OSS_DATABASE, table: $OSS_TABLE..."
    mysql -u oss_admin --password=123 --execute="LOAD DATA LOCAL INFILE \"$file\" INTO TABLE $OSS_TABLE FIELDS TERMINATED BY \"|\"; SHOW WARNINGS;" $OSS_DATABASE

    #mysql --user=$MYSQL_USERNAME --password=$MYSQL_PASSWD --host=$MYSQL_HOST --port=$MYSQL_PORT --execute="LOAD DATA LOCAL INFILE \"$file\" INTO TABLE $OSS_TABLE FIELDS TERMINATED BY \"|\"; SHOW WARNINGS;" $OSS_DATABASE
    echo "$file imported."
    sleep 1
done

